<template>
  <div class="app-container">
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="200px">
        <div class="logo">
          <h2>TEAM04航空X</h2>
          <p>TEAM EASTERN</p>
        </div>
        <el-menu
          :default-active="activeMenu"
          router
          class="side-menu"
          background-color="#304156"
          text-color="#bfcbd9"
          active-text-color="#409EFF"
        >
          <el-submenu index="1" v-if="isAdmin">
            <template #title>
              <i class="el-icon-s-operation"></i>
              <span>航班管理</span>
            </template>
            <el-menu-item index="/flight-info">航班信息</el-menu-item>
            <el-menu-item index="/flight-time">航班时间</el-menu-item>
            <el-menu-item index="/airport-info">机场信息</el-menu-item>
          </el-submenu>

          <el-submenu index="2" v-if="isAdmin">
            <template #title>
              <i class="el-icon-edit"></i>
              <span>航班变更</span>
            </template>
            <el-menu-item index="/flight-change">航班变更</el-menu-item>
            <el-menu-item index="/route-change">航线变更</el-menu-item>
            <el-menu-item index="/route-history">变更历史</el-menu-item>
          </el-submenu>

          <el-menu-item index="/luggage" v-if="isUser">
            <i class="el-icon-suitcase"></i>
            <span>行李转盘</span>
          </el-menu-item>

          <el-menu-item index="/boarding-gate" v-if="isUser">
            <i class="el-icon-guide"></i>
            <span>登机口</span>
          </el-menu-item>

          <el-menu-item index="/account-manage" v-if="isAdmin">
            <i class="el-icon-user"></i>
            <span>账号管理</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <!-- 主内容区 -->
      <el-container>
        <el-header>
          <div class="header-right">
            <span>欢迎，{{ username }}</span>
            <el-button @click="logout" type="text">退出登录</el-button>
          </div>
        </el-header>
        <el-main>
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { computed } from 'vue'
import { useStore } from 'vuex'
import { useRouter, useRoute } from 'vue-router'

export default {
  setup () {
    const store = useStore()
    const router = useRouter()
    const route = useRoute()
    const activeMenu = computed(() => route.path)
    const isAdmin = computed(() => store.getters.isAdmin)
    const isUser = computed(() => store.getters.isUser)
    const username = computed(() => store.state.user.username)
    const logout = () => {
      store.commit('clearUser')
      localStorage.removeItem('token')
      localStorage.removeItem('role')
      router.push('/login')
    }
    return {
      activeMenu,
      isAdmin,
      isUser,
      username,
      logout
    }
  }
}
</script>

<style scoped>
.app-container {
  height: 100vh;
}

.el-container {
  height: 100%;
}

.el-aside {
  background-color: #304156;
  color: #fff;
}

.logo {
  padding: 20px;
  text-align: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.side-menu {
  border-right: none;
}

.el-header {
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.header-right {
  display: flex;
  align-items: center;
}

.header-right span {
  margin-right: 15px;
}

.el-main {
  padding: 20px;
  background-color: #f0f2f5;
}
</style>
